﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NexCart.Web.Models;

namespace NexCart.Web.Repositories {
	public class SqlProductImageRepository : NexCart.Web.Repositories.IProductImageRepository {

		NexCartDataContext db;

		public SqlProductImageRepository(NexCartDataContext db) {
			this.db = db;
		}

		public IQueryable<ProductImage> ProductImages(int productId) {
			return db.ProductImages.Where(m => m.ProductId == productId);
		}

		public ProductImage Save(ProductImage productImage) {
			if (productImage.Id == 0)
				db.ProductImages.InsertOnSubmit(productImage);

			db.SubmitChanges();

			return productImage;
		}

		public ProductImage Single(int id) {
			return db.ProductImages.SingleOrDefault(p => p.Id == id);
		}

		public void Delete(int id) {
			db.ProductImages.DeleteOnSubmit(Single(id));
			db.SubmitChanges();
		}
	}
}
